<html>
<meta charset="utf-8">
<title>套用红头</title>
</head>

<body onload="getAllTemplelists()">
<script type="text/javascript" src='js/main.js'></script>
<select id="templates" size=5 style='width:360px'>
    <!--这里ajax动态拉取。-->
</select>
<br/>
<br/>
<button type="button" onClick="selectTemplate()">套红头</button>
</body>

</html>

<script>

    function selectTemplate() {
        var wpsApp = wps.WpsApplication();
        var obj = document.getElementById("templates");
        var index = obj.selectedIndex;
        if (index == -1) { //添加未选中数据时的异常处理
            alert("请先选择红头文件后再进行套红头！");
            return;
        }
        var templateId = obj.options[index].getAttribute("value");
        var activeDoc = wpsApp.ActiveDocument;
        var docFillDataUrl = GetDocParamsValue(activeDoc, constStrEnum.docFillDataUrl);
        var base = wps.PluginStorage.getItem("getRedHeadPath") || OA_DOOR.getRedHeadPath;
        if (base == undefined) {
            SetDocParamsValue(activeDoc, "insertFileUrl", getDemoTemplatePath());
            SetDocParamsValue(activeDoc, "bkInsertFile", "Content");
            InsertRedHeadDoc(activeDoc);
            window.opener = null;
            window.open('', '_self', '');
            window.close();
            return
        }
        var path = base + templateId;
        SetDocParamsValue(activeDoc, "insertFileUrl", path);
        SetDocParamsValue(activeDoc, "bkInsertFile", "PO_STextS");
        InsertRedHeadDoc(activeDoc);
        GetServerTemplateData(activeDoc, docFillDataUrl + "&templateId=" + templateId);
        window.opener = null;
        window.open('', '_self', '');
        window.close();
    }

    //判断是否是word文档
    function isWord(suffix) {
        var suffixArray = ["doc", "dot", "wps", "wpt", "docx", "docm", "dotm"];
        for (var f1 in suffixArray) {
            if (suffixArray[f1].indexOf(suffix) > -1) {
                return true;
            }
        }
        return false;
    }

    function getAllTemplelists() {
        var xmlhttp = new XMLHttpRequest();
        var redHeadsPath = wps.PluginStorage.getItem("redHeadsPath") || OA_DOOR.redHeadsPath
        xmlhttp.open("POST", redHeadsPath, true); //以POST方式请求该接口
        xmlhttp.onreadystatechange = function () {
            //当接受到响应时回调该方法
            if (xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 0)) {
                var text = xmlhttp.responseText; //使用接口返回内容，响应内容
                console.log(text);
                var resultJson = JSON.parse(text) //将json字符串转换成对象
                var list = resultJson.list;
                console.log(list);
                for (var i = 0; i < list.length; i++) {
                    var element = list[i]
                    var myOption = document.createElement("option"); //动态创建option标签
                    myOption.value = element.templateId; //红头文档id
                    myOption.text = element.templateName; //红头文档名称
                    templates.add(myOption);
                }
            }
        }
        xmlhttp.send(); //发送请求参数间用&分割
    }

    function search() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
            //当接受到响应时回调该方法
            if (xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 0)) {
                var text = xmlhttp.responseText; //使用接口返回内容，响应内容
                var resultJson = JSON.parse(text) //将json字符串转换成对象
                templates.options.length = 0;
                for (var i = 0; i < resultJson.length; i++) {
                    var element = resultJson[i]
                    var myOption = document.createElement("option"); //动态创建option标签
                    myOption.value = element.tempId; //红头文档id
                    myOption.text = element.tempName; //红头文档名称
                    templates.add(myOption);
                }
            }
        }

        var searchPath = wps.PluginStorage.getItem("searchRedHeadPath") || OA_DOOR.redHeadsPath

        var totalPath = searchPath + "?content=" + document.getElementById("content").value;

        xmlhttp.open("get", totalPath, true); //以POST方式请求该接口
        xmlhttp.setRequestHeader("Content-type",
            "application/x-www-form-urlencoded;charset=UTF-16LE"); //添加Content-type
        xmlhttp.send(); //发送请求参数间用&分割
    }

</script>
